﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerDonViTinhDao : IDonViTinhDao
    {
        public SqlServerDonViTinhDao()
        {
            SQLUtility.TableName = "DonViTinh";
        }

        //Trả về đối tượng báo ăn
        private DonViTinh MakeDonViTinh(DataRow row)
        {
            int _id = int.Parse(row["Id"].ToString());
            string _tenDonViTinh = row["TenDonViTinh"].ToString();


         return new DonViTinh(_id, _tenDonViTinh);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<DonViTinh> MakeDonViTinhs(DataTable dt)  
        {
            IList<DonViTinh> list = new List<DonViTinh>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeDonViTinh(row));
            }
            return list;
        }
         private DbParameter[] Take(DonViTinh DonViTinh)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@Id", DonViTinh.Id);
            parameters[1] = new SqlParameter("@TenDonViTinh", DonViTinh.TenDonViTinh);
           
            return parameters;
        }
        public IList<Business_Objects.DonViTinh> GetDonViTinh()
        {
            SQLUtility.TableName = "DonViTinh";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeDonViTinhs(dt); 
        }

        public Business_Objects.DonViTinh GetDonViTinhByID(string  DonViTinhID)
        {
            SQLUtility.TableName = "DonViTinh";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", DonViTinhID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeDonViTinh(row);
            }
        }

        public void InsertDonViTinh(Business_Objects.DonViTinh DonViTinh)
        {
            SQLUtility.TableName = "DonViTinh";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(DonViTinh), true);
        }

        public int UpdateDonViTinh(Business_Objects.DonViTinh DonViTinh)
        {
            SQLUtility.TableName = "DonViTinh";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(DonViTinh)); }
            catch { return -1; }
        }

        public int DeleteDonViTinh(Business_Objects.DonViTinh DonViTinh)
        {
            SQLUtility.TableName = "DonViTinh";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(DonViTinh)); }
            catch { return -1; }
        }
    }
}
